﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Modellayer;
using Dblayer;

namespace ControlLayer
{
    public class ShowCtr
    {

        DbShow dbShow;
        DbMovie dbMovie;
        DbSeat dbSeat;
        DbShowRoom dbShowRoom;
        private static ShowCtr instance;

        private ShowCtr()
        {
            try
            {
                dbShow = new DbShow();
                dbMovie = new DbMovie();
                dbSeat = new DbSeat();
                dbShowRoom = new DbShowRoom();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static ShowCtr getInstance()
        {
            if (instance == null)
            {
                instance = new ShowCtr();
            }
            return instance;
        }

        public void createShow(string showTime, ShowRoom showRoom, Movie movie)
        {
            Show show = new Show(movie, showRoom, dbShow.getMax(), showTime);
            List<Seat> seats = new List<Seat>();
            int numberOfSeats = showRoom.numberOfSeats;
            int index = 0;
            while (index < numberOfSeats)
            {
                Seat seat = new Seat(index, show.showId);
                seats.Add(seat);
                index++;
            }
            show.seats = seats;
            dbShow.createShow(show);
            dbSeat.createSeats(show);

        }

        public Seat findSeat(int showId, int seatNumber)
        {
            Show show = dbShow.findShow(showId);
            int index = 0;
            Boolean found = false;
            List<Seat> seats = dbSeat.getAllSeats(show.showId);

            while (index < seats.Count && found == false)
            {
                if (seats.ElementAt(index).seatNumber == seatNumber)
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }
            return seats.ElementAt(index);
        }

        public List<string> findDatesByMovie(string title)
        {
            return dbShow.findDatesByMovie(title);
        }

        public List<Show> findShowsByDate(string date)
        {
            return dbShow.findShowsByDate(date);
        }


        //sets status in seat object to 0, meaning the seat is not locked or reserved
        public void unLockSeat(int showId, int seatNumber)
        {
            Seat seat = findSeat(showId, seatNumber);
            if (seat != null)
            {
                seat.status = 0;
                dbSeat.updateSeat(seat);
            }
        }

        //sets status in seat object to 2, meaning the seat is locked
        //if seat is lock or reserveret, nothing will happen
        public void lockSeat(int showId, int seatNumber)
        {
            Seat seat = findSeat(showId, seatNumber);
            if (seat != null && seat.status != 1 && seat.status != 2)
            {
                seat.status = 2;
                dbSeat.updateSeat(seat);
            }
        }

        //sets status in seat object to 1, meaning the seat is reserved to a reservation
        //if seat is reserveret, nothing will happen
        public void reserveSeat(int showId, int seatNumber, int reservationId)
        {
            Seat seat = findSeat(showId, seatNumber);
            if (seat != null && seat.status != 1)
            {
                seat.status = 1;
                seat.reservationId = reservationId;
                dbSeat.updateSeat(seat);
            }
        }

        public void updateSeat(Seat seat)
        {
            dbSeat.updateSeat(seat);
        }

        public Show findShow(int showId)
        {
            Show show = dbShow.findShow(showId);
            show.seats = dbSeat.getAllSeats(show.showId);
            return show;
        }

        public List<Show> findShowsByTitleAndDate(string dato, string title)
        {
            return dbShow.findShowsByTitleAndDate(dato, title);
        }

        public List<Seat> getAllSeats(int showId)
        {
            return dbSeat.getAllSeats(showId);
        }
    }
}
